package com.lyf.baoxian.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.lyf.baoxian.R;
import com.lyf.baoxian.activity.EditCustomerActivity;
import com.lyf.baoxian.bean.CustomerBaoDan;
import com.lyf.baoxian.bean.CustomerInfo;

import java.util.List;

/**
 * Created by 刘亚飞 on 2016/12/1.
 */

public class CustomerAdaper extends BaseAdapter {
    private List<CustomerInfo> datas;
    private Context context;

    public CustomerAdaper(Context context) {
        this.context = context;
    }


    public void setDatas(List<CustomerInfo> datas){
        this.datas = datas;
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int i) {
        return datas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        CustomerAdaper.ViewHolder holder;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.list_item_customer,null);
            holder = new CustomerAdaper.ViewHolder();
            holder.customerName = (TextView) view.findViewById(R.id.tv_customer_name);
            holder.customerId = (TextView) view.findViewById(R.id.tv_customer_idcar);
            holder.customerPhone = (TextView) view.findViewById(R.id.tv_customer_phone);
            holder.customerAds = (TextView) view.findViewById(R.id.tv_customer_address);
            holder.delCustomer = (Button) view.findViewById(R.id.bt_customer_del);
            holder.edtCustomer = (Button) view.findViewById(R.id.bt_customer_edt);
            view.setTag(holder);
        }else {
            holder = (CustomerAdaper.ViewHolder) view.getTag();
        }

        CustomerInfo customerInfo = datas.get(i);
        holder.customerName.setText(customerInfo.getCustomerName()+"");
        holder.customerId.setText(customerInfo.getCustomerId()+"");
        holder.customerPhone.setText(customerInfo.getPhone()+"");
        holder.customerAds.setText(customerInfo.getAddress()+"");

        //删除
        holder.delCustomer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        //编辑
        holder.edtCustomer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, EditCustomerActivity.class);
                context.startActivity(intent);

            }
        });

        return view;
    }

    class ViewHolder{
        TextView customerName;
        TextView customerId;
        TextView customerPhone;
        TextView customerAds;
        Button edtCustomer;
        Button delCustomer;

    }
}
